<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\Merchant;
use app\modules\v1\models\Merchantinfocard;
use app\modules\v1\models\Merchantextrastuff;
use app\modules\v1\models\MerchantWeixin;

class GetidentityController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\getidentity';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid  = isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    		if(empty($openid)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号';
    			$response			=Yii::$app->response;
    			$response->format	=Response::FORMAT_JSON;
    			$response->data		=$res;
    			return $response;
    		}
    		$weixininfo = MerchantWeixin::findByOpenid($openid);
    		$name				= '';
    		$id_card   			= '';
    		$phone				= '';
    		$merchant_name		= '';
    		$address_province	= '';
    		$address_city		= '';
    		$address_area		= '';
    		$address_detail		= '';
    		$bank_no			= '';
    		$bank_name			= '';
    		$bank_branch		= '';
    		$bank_province		= '';
    		$bank_city			= '';
    		$bank_area			= '';
    		$bank_card_type		= '';
    		$bank_code			= '';
    		$extra_pictures 	= '0';
    		$identity_status	= '0';
    		if(!empty($weixininfo)){
    			$merchant1 		= Merchant::findOne($weixininfo->merchant_id);
    			if(!empty($merchant1)){
    				$name				= !empty($merchant1->merchant_real_name)?$merchant1->merchant_real_name:'';
    				$id_card   			= !empty($merchant1->identity_card)?$merchant1->identity_card:'';
    				$phone				= !empty($merchant1->mobile)?$merchant1->mobile:'';
    				if(empty($merchant1->merchant_name)){
    					$merchant_name  = base64_decode($weixininfo->nickname);
    				}else{
    					$merchant_name 	= $merchant1->merchant_name;
    				}
    				$address_province	= !empty($merchant1->province)?$merchant1->province:'';
    				$address_city		= !empty($merchant1->city)?$merchant1->city:'';
    				$address_area		= !empty($merchant1->county)?$merchant1->county:'';
    				$address_detail		= !empty($merchant1->detail_address)?$merchant1->detail_address:'';
    				$merchant_no	= !empty($merchant1->merchant_no)?$merchant1->merchant_no:'';
    				//认证信息  0未认证；1认证正在审核；2认证未通过；3认证通过
    				$identity_status    = $merchant1->check_status_jinjian;	
    			}
    			$merchant2		= Merchantinfocard::findOne(['merchant_id' => $weixininfo->merchant_id]);
    			if(!empty($merchant2)){
    				$bank_no			= !empty($merchant2->account_no)?$merchant2->account_no:'';
    				$bank_name			= !empty($merchant2->bank_name)?$merchant2->bank_name:'';
    				$bank_branch		= !empty($merchant2->bank_branch)?$merchant2->bank_branch:'';
    				$bank_province		= !empty($merchant2->bank_province)?$merchant2->bank_province:'';
    				$bank_city			= !empty($merchant2->bank_city)?$merchant2->bank_city:'';
    				$bank_area			= !empty($merchant2->bank_county)?$merchant2->bank_county:'';
    				$bank_card_type		= !empty($merchant2->bank_card_type)?$merchant2->bank_card_type:'';
    				$bank_code			= !empty($merchant2->bank_code)?$merchant2->bank_code:'';
    			}
    			
    			$merchant3		= Merchantextrastuff::find()->where(['merchant_id' => $weixininfo->merchant_id,'state'=>0])->count();
    			$extra_pictures = $merchant3;
    		}else{
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号';
    			$response			=Yii::$app->response;
    			$response->format	=Response::FORMAT_JSON;
    			$response->data		=$res;
    			return $response;
    		}
    		if(!empty($id_card)){
    			$id_card = substr_replace($id_card,'***********',3,11);
    		}
    		if(!empty($bank_no)){
    			$bank_no = substr_replace($bank_no,'**********',3,10);
    		}
    		if(!empty($phone)){
    			$phone 	= substr_replace($phone,'*****',3,5);
    		}
    		
    		$res['data'] = [
    			'openid'  			=> $openid,
    			'name'	  			=> $name,
    			'id_card'	  		=> $id_card,
    			'phone'	  			=> $phone,
    			'merchant_name'	  	=> $merchant_name,
    			'address_province'	=> $address_province,
    			'address_city'	  	=> $address_city,
    			'address_area'	  	=> $address_area,
    			'address_detail'	=> $address_detail,
    			'bank_no'	  		=> $bank_no,
    			'bank_name'	  		=> $bank_name,
    			'bank_branch'	  	=> $bank_branch,
    			'bank_province'	  	=> $bank_province,
    			'bank_city'	  		=> $bank_city,
    			'bank_area'	  		=> $bank_area,
    			'bank_card_type'	=> $bank_card_type,
    			'bank_code'			=> $bank_code,
    			'is_extra_pictures'	=> $extra_pictures,
    			'identy_status'		=> $identity_status,
    			'merchant_no'		=> $merchant_no,
    		];
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	}else{
    		$res['code'] = '10402';
    		$res['msg']  = '取消关注后,再重新关注公众号';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	} 
    }

}